package com.evan.kdl.test.day01;

import com.evan.kdl.util.CommonUtils;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @author huangzhongliang
 * @time 2023/12/27 18:28
 */
public class FutureDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {


        ExecutorService executor = Executors.newFixedThreadPool(5);
        //    读敏感文件 有返回
        Future<String[]> filterWordFuture = executor.submit(() -> {
            String s = CommonUtils.readFile("F:\\idea-workspace-personal\\juc-springboot-demo\\src\\main\\resources\\filter_words.txt");
            String[] filterWords = s.split(",");
            return filterWords;
        });


        //    读目标替换文件
        Future<String> newsFuture = executor.submit(() -> {
            String s = CommonUtils.readFile("F:\\idea-workspace-personal\\juc-springboot-demo\\src\\main\\resources\\news.txt");
            return s;
        });

        //    脱敏
        Future<String> replaceFuture = executor.submit(() -> {
            String[] words = filterWordFuture.get();
            String news = newsFuture.get();
            for (String word : words) {
                if (news.contains(word)) {
                    news = news.replace(word, "**");
                }

            }
            return news;
        });

        String filteredNews = replaceFuture.get();
        System.out.println("过滤后的新闻稿:" + filteredNews);
        executor.shutdown();
    }
}
